package StudyCode.summary.csv;

import java.util.Iterator;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CSVRecordFormatter {
	private static <T extends Object> String formatCsvRecord(
			List<T> csvRecord, char csvSeparator) {

		StringBuilder fields = new StringBuilder();
		String separator = String.valueOf(csvSeparator);

		for (Iterator<T> iter = csvRecord.iterator(); iter.hasNext(); ) {
			T object = iter.next();

			if (object != null) {
				String field = object.toString().replace("\"", "\"\"");

				if (field.contains(separator) || field.contains("\"")) {
					field = "\"" + field + "\"";
				}

				fields.append(field);
			}

			if (iter.hasNext()) {
				fields.append(separator);
			}
		}

		return fields.toString();
	}
}
